package com.jojo.android.zxlib.tool;

import android.util.Log;


import java.io.File;
import java.io.IOException;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okio.Buffer;
import okio.BufferedSink;
import okio.Okio;
import okio.Source;

public
class ZxUploadTool {

    /**
     * @param url
     * @param file  文件
     * @param progressListener  进度回调
     * @param filePartName     文件部分 字段名
     * @param parts            携带的字段
     * @return
     */
    public static String uploadFile(String url, File file, final ProgressListener progressListener, String filePartName, Map<String, String> parts) {
        try {
            OkHttpClient client = new OkHttpClient();
            MultipartBody.Builder builder = new MultipartBody.Builder()
                    .setType(MultipartBody.FORM)
                    .addFormDataPart(filePartName, file.getName(),
                            createCustomRequestBody(MultipartBody.FORM, file, progressListener));
            if (parts != null) {
                for (String n : parts.keySet()
                ) {
                    builder.addFormDataPart(n, parts.get(n));
                }
            }
            RequestBody requestBody = builder.build();
            Request request = new Request.Builder()
                    .url(url)
                    .post(requestBody)
                    .build();
            return client.newCall(request).execute().body().string();
        } catch (Exception e) {
            ZxLogTool.i("ZxUploadTool", Log.getStackTraceString(e));
        }
        return null;

    }

    public static RequestBody createCustomRequestBody(final MediaType contentType,
                                                       final File file, final ProgressListener listener) {
        return new RequestBody() {
            @Override
            public MediaType contentType() {
                return contentType;
            }

            @Override
            public long contentLength() {
                return file.length();
            }

            @Override
            public void writeTo(BufferedSink sink) throws IOException {
                Source source;
                try {
                    source = Okio.source(file);
                    //sink.writeAll(source);
                    Buffer buf = new Buffer();
                    Long remaining = contentLength();
                    for (long readCount; (readCount = source.read(buf, 2048)) != -1; ) {
                        sink.write(buf, readCount);
                        if (listener != null)
                            listener.onProgress(contentLength(), remaining -= readCount, remaining == 0);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
    }

    public interface ProgressListener {
        void onProgress(long totalBytes, long remainingBytes, boolean done);
    }


}
